package com.ouheng.shop.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouheng.common.utils.PageUtils;
import com.ouheng.common.utils.Query;
import com.ouheng.common.utils.R;
import com.ouheng.shop.domain.MemberDO;
import com.ouheng.shop.domain.OhOrderDO;
import com.ouheng.shop.domain.ReceiverDO;
import com.ouheng.shop.service.MemberService;
import com.ouheng.shop.service.OhOrderService;
import com.ouheng.shop.service.ReceiverService;

/**
 * 会员用户
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-09-04 10:39:38
 */
 
@Controller
@RequestMapping("/shop/member")
public class MemberController {
	@Autowired
	private MemberService memberService;
	
	@Autowired
	private OhOrderService ohOrderService;
	
	@Autowired
	private ReceiverService receiverService;
	
	
	@GetMapping()
	@RequiresPermissions("shop:member:member")
	String Member(){
	    return "shop/member/member";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("shop:member:member")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MemberDO> memberList = memberService.list(query);
		List<MemberDO> memberLists = new ArrayList<MemberDO>();
		for (MemberDO memberDO : memberList) {
			Map<String, Object> map =new HashMap<>();
			map.put("memberid", memberDO.getId());
			int count = memberService.memberRelationCount(memberDO.getId());
			BigDecimal consume= new BigDecimal(0.00);
			List<OhOrderDO> orderList = ohOrderService.listOrderByMemberid(map);
			for (OhOrderDO ohOrderDO : orderList) {
				consume=consume.add(ohOrderDO.getPaymoney());
			}
			memberDO.setConsume(consume);
			int orderCount = ohOrderService.listOrderByMemberidCount(map);
			memberDO.setMemberRelationCount(count);
			memberDO.setOrderCount(orderCount);
			memberLists.add(memberDO);
		}
		int total = memberService.count(query);
		PageUtils pageUtils = new PageUtils(memberLists, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("shop:member:add")
	String add(){
	    return "shop/member/add";
	}

	@GetMapping("/edit/{id}")
//	@RequiresPermissions("shop:member:edit")
	String edit(@PathVariable("id") Long id,Model model){
		MemberDO member = memberService.get(id);
		Map<String, Object> map =new HashMap<>();
		map.put("memberid", id);
		List<ReceiverDO> listReceiver = receiverService.list(map);
		model.addAttribute("listReceiver", listReceiver);
		BigDecimal consume= new BigDecimal(0.00);
		List<OhOrderDO> orderList = ohOrderService.listOrderByMemberid(map);
		for (OhOrderDO ohOrderDO : orderList) {
			consume=consume.add(ohOrderDO.getPaymoney());
		}
		member.setConsume(consume);
		model.addAttribute("member", member);
	    return "shop/member/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("shop:member:add")
	public R save( MemberDO member){
		if(memberService.save(member)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("shop:member:edit")
	public R update( MemberDO member){
		memberService.update(member);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("shop:member:remove")
	public R remove( Long id){
		if(memberService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("shop:member:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		memberService.batchRemove(ids);
		return R.ok();
	}
	
	/**
	 * 客户粉丝
	 * @Title: memberRelation   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param id
	 * @param: @return      
	 * @return: R      
	 * @throws
	 */
	@GetMapping("/memberRelation")
	public String memberRelation(Long id,Model model){
		model.addAttribute("pmemberid", id);
		return "shop/member/memberRelation";
	}
	
	/**
	 * 客户粉丝
	 * @Title: listMemberRelation   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param params
	 * @param: @return      
	 * @return: PageUtils      
	 * @throws
	 */
	@GetMapping("/listMemberRelation")
	@ResponseBody
	public PageUtils listMemberRelation(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<MemberDO> memberList = memberService.listMemberRelation(query);
		int total = memberService.listMemberRelationCount(query);
		PageUtils pageUtils = new PageUtils(memberList, total);
		return pageUtils;
	}
	
	
	@PostMapping( "/removeRelation")
	@ResponseBody
	@RequiresPermissions("shop:member:removeRelation")
	public R removeRelation( Long id){
		if(memberService.removeRelation(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 客户成交订单
	 * @Title: order   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param id
	 * @param: @param model
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	@GetMapping("/order/{id}")
	public String order(@PathVariable("id")Long id,Model model){
		model.addAttribute("memberid", id);
		return "shop/member/ohOrder";
	}
	
	
	/**
	 * 客户成交订单
	 * @Title: listMemberRelation   
	 * @Description: TODO(这里用一句话描述这个方法的作用)   
	 * @param: @param params
	 * @param: @return      
	 * @return: PageUtils      
	 * @throws
	 */
	@GetMapping("/listOrder")
	@ResponseBody
	public PageUtils listOrder(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<OhOrderDO> orderList = ohOrderService.listOrderByMemberid(query);
		List<OhOrderDO> OhOrderDOList2 = new ArrayList<>();
		for (OhOrderDO ohOrderDO : orderList) {
			ohOrderDO.setProductNameAndNumber(ohOrderService.orderProductNameAndNumber(ohOrderDO.getId()));
			OhOrderDOList2.add(ohOrderDO);
		}
		int total = ohOrderService.listOrderByMemberidCount(query);
		PageUtils pageUtils = new PageUtils(orderList, total);
		return pageUtils;
	}
	
	@ResponseBody
	@GetMapping("/listReceiver")
	public PageUtils listReceiver(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ReceiverDO> receiverList = receiverService.list(query);
		int total = receiverService.count(query);
		PageUtils pageUtils = new PageUtils(receiverList, total);
		return pageUtils;
	}
	
}
